import { Modal } from 'antd-mobile'
const alert = Modal.alert
const request = (url, config) => {
    return fetch(url, config).then((res) => {
        if (!res.ok) {
            // 服务器异常返回
            alert('温馨提示', '服务器发生故障，请稍后重试', [
                { text: '确定', onPress: () => console.log('') }
            ]);
        }

        return res.json();
    }).then((resJson) => {
        return resJson;
    }).catch((error) => {
        alert('温馨提示', '服务器发生故障，请稍后重试', [
            { text: '确定', onPress: () => console.log('') }
        ]);
    });
};

// GET请求
export const get = (url) => {
    return request(url, { method: 'GET' });
};

// POST请求
export const post = (url, data) => {
    let str = ''
    for (var key in data) {
        let newstr = ''
        newstr += key + '=' + data[key] + '&'
        str += newstr
    }
    str=str.substring(0,str.length-1)
    return request(url, {
        body: str,
        headers: {
            "content-Type": "application/x-www-form-urlencoded;charset=utf-8"
        },
      //   mode: "no-cors",
        method: 'POST'
    });
};